package com.sznaner.tool.util;


import android.content.Context;

import java.io.BufferedWriter;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class BufferedWriterUtil {

//    adb写入节点
//    echo local>/dev/aux_device
//    adb读取节点
//    cat /dev/aux_device

    public static String path_aux = "/dev/aux_device";//写入字符串aux切换到外部音源，写入local切换到本地音源
    public static String path_hdmi = "/dev/hdmi_device";//写入字符串hdmi切换到hdmi播放，写入local切换到本地小屏播放

    //px30_4寸
    public static String path_aux_px30 = "/proc/vendor/aux_mode";//默认local,写入aux切到外部音源
    public static String path_mute_px30 = "/proc/vendor/mute_mode";//默认on,静音on, off非静音(系统有声音输出,会自动取消静音)

    //写入节点
    public static void write(String path,String str){
        if(path == null || str == null) return;
        try{
            BufferedWriter bufWriter = null;
            bufWriter = new BufferedWriter(new FileWriter(path));
            bufWriter.write(str);
            bufWriter.close();
        }catch(IOException e) {
            e.printStackTrace();
        }
    }

    //读取指定长度节点值
    public static String readLength(String path,int length) {
        String result = "";
        if(path == null) return result;
        if(length == 0) length = 10;//默认
        try{
            MyBufferedReader reader = new MyBufferedReader(new FileReader(path));
            result = reader.readLength(length);
            LogUtil.debugLog("节点值:"+result);
        }catch(IOException e){
            e.printStackTrace();
        }
        return result;
    }

    //get
    public static Boolean isSourceLocal( ){
        String path = ScreenUtil.is4() ? BufferedWriterUtil.path_aux_px30 :  BufferedWriterUtil.path_aux;
        if(BufferedWriterUtil.readLength(path,10).contains("local")){//本地音源
           return true;
        }
        return false;
    }
    public static Boolean isSourceAux(){
        String path = ScreenUtil.is4() ? BufferedWriterUtil.path_aux_px30 :  BufferedWriterUtil.path_aux;
        if(BufferedWriterUtil.readLength(path,10).contains("aux")){//aux音源
            return true;
        }
        return false;
    }

    public static boolean isHdmiOn(){
        if(BufferedWriterUtil.readLength(BufferedWriterUtil.path_hdmi,10).contains("hdmi")){//aux音源
            return true;
        }
        return false;
    }

    //set
    public static void setSourceLocal(){
        if(isSourceLocal()) return;
        String path = ScreenUtil.is4() ? BufferedWriterUtil.path_aux_px30 :  BufferedWriterUtil.path_aux;
        BufferedWriterUtil.write(path,"local");
        LogUtil.debugLog("aux设置:local");
    }
    public static void setSourceAux(){
        if(isSourceAux()) return;
        String path = ScreenUtil.is4() ? BufferedWriterUtil.path_aux_px30 :  BufferedWriterUtil.path_aux;
        BufferedWriterUtil.write(path,"aux");
        LogUtil.debugLog("aux设置:aux");
    }
    public static void setHdmiOn(Boolean on){
        if(on){
            BufferedWriterUtil.write(BufferedWriterUtil.path_hdmi,"hdmi");
        }else {
            BufferedWriterUtil.write(BufferedWriterUtil.path_hdmi,"local");
        }
    }







    public static class MyBufferedReader {

        private FileReader f;

        // 定义一个数组作为缓冲区
        private char[] buf = new char[1024];
        // 定义一个指针用来操作数组中的元素，当操作到最后一个元素后指针归零
        private int pos = 0;
        // 定义一个计数器用来计算缓冲区中的数据个数，当为0时，继续取数据到缓冲区中
        private int count = 0;

        public MyBufferedReader(FileReader f) {
            this.f = f;
        }

        /**
         * 缓冲区中读
         * @return
         * @throws IOException
         */
        public int read() throws IOException {
            // 源中取数据到缓冲区中,计数器为0才取
            if(count==0){
                count = f.read(buf);
                pos = 0;
            }

            if(count==-1){
                return -1;
            }
            char ch = buf[pos];
            pos++;
            count--;

            return ch;
        }

        //读取一行
        public String readLine() throws IOException {
            //当一个缓冲区
            StringBuilder sb = new StringBuilder();

            int ch = 0;
            while((ch = read())!=-1){
                if(ch == '\r'){
                    continue;
                }
                if(ch=='\n'){
                    return sb.toString();
                }
                sb.append((char)ch);
            }
            //最后一行后面没符号，也成功
            if(sb.length()!=0)
                return sb.toString();
            return null;
        }

        //读取指定长度
        public String readLength(int length) throws IOException {
            //当一个缓冲区
            StringBuilder sb = new StringBuilder();
            int ch = 0;
            for (int i= 0; i<length;i++){
                ch = read();
                if(ch != -1){
                    sb.append((char)ch);
                }
            }
            return sb.toString();
        }

        public void close() throws IOException {
            f.close();
        }

    }



}
